package com.ego.controller;

import com.ego.dao.CartDao;
import com.ego.dao.impl.CartDaoImpl;
import com.ego.vo.CartItemVo;
import com.ego.vo.CartListVo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author 袁梦达 2019012364
 */
@WebServlet("/CartShowServlet")
public class CartShowServlet extends HttpServlet {

    private CartDao cartDao = new CartDaoImpl();

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String userNumber = String.valueOf(getServletContext().getAttribute("userNumber"));
        if(userNumber == null){
            //未登录
            response.getWriter().print("");
        }else {
            CartListVo cart = cartDao.showCart(userNumber);
            double sumPrice = cart.getSumPrice();
            List<CartItemVo> cartItemVoList = cart.getCartItemVoList();
            StringBuffer msg = new StringBuffer();
            msg.append(sumPrice).append("#");
            for (CartItemVo cartItemVo : cartItemVoList) {
                msg.append(cartItemVo.getImage()).append("@")
                        .append(cartItemVo.getBaseInformation()).append("@")
                        .append(cartItemVo.getPrice()).append("@")
                        .append(cartItemVo.getQuantity()).append("@")
                        .append(cartItemVo.getSumPrice()).append("@")
                        .append(cartItemVo.getProductId()).append("#");
            }
            response.getWriter().print(msg.toString());
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
